// VideoSocket.cpp: implementation of the CVideoSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "VideoSocket.h"
#include "WinSock2.h"
#pragma     comment(lib, "ws2_32.lib") 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVideoSocket::CVideoSocket()
{

}

CVideoSocket::~CVideoSocket()
{

}

bool CVideoSocket::InitUDPSocket(char *ip, short port)
{
	WSADATA  Ws;
	SOCKET ServerSocket;
	struct sockaddr_in LocalAddr;
	if(WSAStartup(MAKEWORD(2,2), &Ws) != 0 ) 
	{
		return false;
	}
	ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_UDP); 
	if ( ServerSocket == INVALID_SOCKET ) 
    {
		  return false;
	}
	LocalAddr.sin_family = AF_INET; 
	if(ip==NULL)
	{
		LocalAddr.sin_addr.s_addr   =   htonl(INADDR_ANY);
	}
	else
	{
		LocalAddr.sin_addr.s_addr = inet_addr(ip); 
	}
	LocalAddr.sin_port = htons(port); 
	int Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr)); 
	if(Ret!=0)
	{
		closesocket(ServerSocket);  
        WSACleanup(); 
		return false;
	}
	return true;
}
bool CVideoSocket::InitTCPSocket(char *ip,short port)
{
	WSADATA  Ws;
	SOCKET ServerSocket;
	struct sockaddr_in LocalAddr;
	if(WSAStartup(MAKEWORD(2,2), &Ws) != 0 ) 
	{
		return false;
	}
	ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
	if (ServerSocket==INVALID_SOCKET) 
    {
		 return false;
	}
	LocalAddr.sin_family = AF_INET; 
	if(ip==NULL)
	{
		LocalAddr.sin_addr.s_addr=htonl(INADDR_ANY);
	}
	else
	{
		LocalAddr.sin_addr.s_addr=inet_addr(ip); 
	}
	LocalAddr.sin_port = htons(port); 
	int Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr)); 
	if(Ret!=0)
	{
		closesocket(ServerSocket);  
        WSACleanup(); 
		return false;
	}
	return true;
}

